package pers.yurwisher.dota2.pudge.system.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pers.yurwisher.dota2.pudge.base.BasePageQo;
import pers.yurwisher.dota2.pudge.wrapper.PageR;
import pers.yurwisher.dota2.pudge.wrapper.R;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/escm")
public class EscmController {

    private final static Logger logger = LoggerFactory.getLogger(EscmController.class);


    private JSONObject json;

    public EscmController(@Value(value = "classpath:data.json") Resource resource) throws Exception {
        json = JSON.parseObject(resource.getInputStream(), Charset.defaultCharset(), JSONObject.class);
    }

    @GetMapping("bookingOrder")
    public R<Object> bookingOrder(BasePageQo qo) {
        JSONArray orders = json.getJSONArray("orderList");
        PageR<Object> pageR = new PageR<>();
        pageR.setPages(1);
        pageR.setTotal(orders.size());
        pageR.setRows(orders);
        return R.ok(pageR);
    }

    @GetMapping("settlement")
    public R<Object> settlement() {
        return R.ok(json.getJSONArray("settlementList"));
    }

    @GetMapping("supplier")
    public R<Object> supplier(String keyword) {
        return R.ok(json.getJSONArray("supplierList"));
    }

    @GetMapping("supplierContact")
    public R<Object> supplierContact(@RequestParam String supplierId) {
        return R.ok(json.getJSONArray("supplierContactList"));
    }

    @GetMapping("supplierBank")
    public R<Object> supplierBank(@RequestParam String supplierId) {
        return R.ok(json.getJSONArray("supplierBankList"));
    }

    @GetMapping("pickAddress")
    public R<Object> pickAddress(@RequestParam String supplierId) {
        return R.ok(json.getJSONArray("pickAddressList"));
    }

    @GetMapping("deliveryAddress")
    public R<Object> deliveryAddress() {
        return R.ok(json.getJSONArray("deliveryAddressList"));
    }

    @GetMapping("material")
    public R<Object> material(@ModelAttribute BasePageQo qo) {
        JSONArray array = json.getJSONArray("items");
        if (StrUtil.isNotBlank(qo.getKeyword())) {
            List<JSONObject> list = new ArrayList<>(array.size());
            for (int i = 0; i < array.size(); i++) {
                if (array.getJSONObject(i).toJSONString().contains(qo.getKeyword())) {
                    list.add(array.getJSONObject(i));
                }
            }
            return R.ok(list);
        }
        return R.ok(array);
    }

    @GetMapping("orderLogistics")
    public R<Object> orderLogistics(@RequestParam String orderNo) {
        return R.ok(json.getJSONArray("orderLogistics"));
    }
}
